package ControleDiversas;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import JBCD.Conexao;
import JBCD.MetodoConexao;
import ModeloAcademico.ProfessoeBeen;
import VisaoControleAcademico.NovaTurma;
import VisaoControleAcademico.TelaDoProfessor;

public class ProfessorDao {
	private ProfessoeBeen contato;
	private Connection   conexao;
	 
	
	public ProfessorDao(){
		
		contato = new ProfessoeBeen();
		conexao = new Conexao().getConnection();
	}
	
	public void adicionaContato(ProfessoeBeen c) throws SQLException{
		String sql = "INSERT INTO professor (nome,endereco,email,cpf,rg,curso,datanascimento) values (?,?,?,?,?,?,?)";
		
		PreparedStatement stmt = conexao.prepareStatement(sql);
		stmt.setString(1, c.getNome());
		stmt.setString(2, c.getEndereco());
		stmt.setString(3, c.getEmail());
		stmt.setString(4, c.getCpf());
		stmt.setString(5, c.getRg());
		stmt.setString(6, c.getCurso());
		stmt.setString(7, c.getData());
		stmt.execute();
		stmt.close();
		
	} 
	
	public static void pesquisa(){
		TelaDoProfessor.combo.removeAllItems();
		MetodoConexao.conctarBanco();
		try {
			ResultSet resultado_RS = MetodoConexao.coneck.executeQuery("SELECT * FROM curso ORDER BY nome");
			while (resultado_RS.next()) {
				TelaDoProfessor.combo.addItem(resultado_RS.getString("nome").trim());
						}
			MetodoConexao.con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		ProfessoeBeen c = new ProfessoeBeen();
		c.setId(1);
		c.setNome("Paulo");
		c.setEndereco("Bom jardim");
		c.setEmail("oi@hotmail.com");
		c.setCpf("12312312");
		c.setRg("241234123");
		c.setCurso("SI");
		c.setData("12/12/1992");
		ProfessorDao d = new ProfessorDao();
		try {
			d.editaContato(c);
			System.out.println("Alterado com sucesso!");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
//------------------------------------------------------------------------------------------
	public void editaContato(ProfessoeBeen c) throws SQLException{
		

		String sql = "UPDATE professor set  nome=? ,endereco=?,email=?,cpf=?,rg=?,curso=?,datanascimento =?  where id = ?  ";
		PreparedStatement stmt = conexao.prepareStatement(sql);
		stmt.setLong(8, (new Long(c.getId())));
		stmt.setString(1, c.getNome());
		stmt.setString(2, c.getEndereco());
		stmt.setString(3, c.getEmail());
		stmt.setString(4, c.getCpf());
		stmt.setString(5, c.getRg());
		stmt.setString(6, c.getCurso());
		stmt.setString(7, c.getData());
		stmt.execute();
		stmt.close();

	}
	//---------------------------------------------------------------------------------------------
	public List<ProfessoeBeen> pesquisar() throws SQLException {
		List<ProfessoeBeen> contatos = new ArrayList<ProfessoeBeen>();
		
			String sql = "SELECT * FROM professor";
			PreparedStatement stmt = conexao.prepareStatement(sql);

            ResultSet set = stmt.executeQuery();
			while (set.next()) {
				ProfessoeBeen instituicao = new ProfessoeBeen();
				instituicao.setId(Long.valueOf(set.getString("id")));
				instituicao.setNome(set.getString("nome"));

				instituicao.setEndereco(set.getString("endereco"));
				instituicao.setEmail(set.getString("email"));
				instituicao.setCpf(set.getString("cpf"));
				instituicao.setRg(set.getString("rg"));
				//instituicao.setEmail(set.getString("rg"));
				instituicao.setCurso(set.getString("curso"));
				instituicao.setData(set.getString("datanascimento"));
				
				contatos.add(instituicao);
			}
			return contatos;
		}
			
		
	//---------------------------------------------------------------------------------------------

}
